<?php
/**
 * This file is part of php-agenda.
 * 
 * php-agenda is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * php-agenda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with php-agenda; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Copyright 2006, Thomas Abeel
 * 
 * Project: http://sourceforge.net/projects/php-agenda/
 * 
 */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
  <title>Install the Simple PHP Agenda</title>
  <style type="text/css">
body {
  font-family: georgia;
  margin: 2em;    
}
div label {
  display: block;
  font-size: 0.8em;
}
div {
  margin-bottom: 0.5em;    
}
input.invalid {
  background-color: pink;
}
strong.error {
  color: red;
}
  </style>
</head>
<body>
<h1>Install the Simple PHP Agenda</h1>
<?php
include ("adodb/adodb.inc.php");
if(isset($_POST["dbhost"])){
	$data = '<?php' . "\n\t";
	$data .= '$dbhost=\'' . $_POST["dbhost"] . "';\n\t";
	$data .= '$dbname=\'' . $_POST["dbname"] . "';\n\t";
	$data .= '$dbuser=\'' . $_POST["dbuser"] . "';\n\t";
	$data .= '$dbpass=\'' . $_POST["dbpass"] . "';\n";
	//$data.='$table_prefix=\''.$_POST["tblPrefix"]."';\n";
	$data .= '?>';

	$temp = 'local_config_inc.php'; #define temporary target name
	$fp = fopen($temp, "w", 0) or die("Installation failed. Make sure local_config_inc.php is writable by the webserver."); #open for writing
	fputs($fp, $data); #write all of $data to our opened file
	fclose($fp); #close the file
	echo "Config file was succesfully created...";
	include ('local_config_inc.php');
	//create database
	$db = ADONewConnection('mysql'); # eg 'mysql' or 'postgres'
	$db->debug = true;
	$user = $_POST["user"];
	$pass = md5($_POST["pass1"]);

	$db->Connect($dbhost, $dbuser, $dbpass);
	$db->Execute("Create database if not exists $dbname") or die($db->ErrorMsg());
	$db->SelectDB($dbname);
	$db->Execute("CREATE TABLE `users` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`display_name` VARCHAR( 200 ) NOT NULL ,`md5pass` CHAR( 32 ) NOT NULL);");
	$sql = "INSERT INTO `users` ( `display_name` , `md5pass` )VALUES ('$user', '$pass');";
	$db->Execute($sql);
	$sql = "CREATE TABLE `events` (
		  `id` int(11) NOT NULL auto_increment,
		  `user_id` int(11) NOT NULL default '0',
		  `date` int(11) NOT NULL default '0',
		  `title` text character set utf8 collate utf8_roman_ci NOT NULL,
		  `description` text character set utf8 collate utf8_roman_ci NOT NULL,
		  `added` int(11) NOT NULL default '0',
		  `status` tinyint(4) NOT NULL default '0',
		  `deadline` tinyint(4) NOT NULL default '0',
		  `dayevent` tinyint(4) NOT NULL default '0',
		  PRIMARY KEY  (`id`),
		  KEY `user_id` (`user_id`)
		)";
	$db->Execute($sql);
	$sql = "CREATE TABLE `todo` (
	  `id` int(11) NOT NULL auto_increment,
	  `user_id` int(11) NOT NULL default '0',
	  `priority` tinyint(4) NOT NULL default '0',
	  `text` text NOT NULL,
	  `added` int(11) NOT NULL default '0',
	  `status` tinyint(4) NOT NULL default '0',
	  `closed` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`id`),
	  KEY `user_id` (`user_id`)
	)";
	$db->Execute($sql);
	$db->Close();
	//create tables
} 
?>

<form action="install.php" method="post">
<label for="dbhost">Host name:</label>
<input type="text" name="dbhost"/><br/>
<label for="dbname">Database name:</label>
<input type="text" name="dbname"/><br/>
<label for="dbuser">Database username:</label>
<input type="text" name="dbuser"/><br/>
<label for="dbpass">Database password:</label>
<input type="password" name="dbpass"/><br/>
<label for="user">Admin agenda user:</label>
<input type="text" name="user"/><br/>
<label for="pass1">Admin agenda pass:</label>
<input type="password" name="pass1"/><br/>
<label for="pass2">Confirm pass:</label>
<input type="password" name="pass2"/><br/>
<div><input type="submit" value="Create Agenda" /></div>
</form>



</body>
</html>